package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.dsp.bank.directbank.bankInterface.IPrivateSaleMsg;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.privatesale.ReqPrivateSaleMsgQry;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleDetail;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleMsg;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取私售信息
 * @author Parker on 2020-05-22.
 * @version 1.0
 */
public class CIB02PrivateSaleMsg  extends CIB02Base implements IPrivateSaleMsg {
    @Override
    public RetAtsPrivateSaleMsg getPrivateSaleMsg(ReqPrivateSaleMsgQry qry) throws Exception {
        RetAtsPrivateSaleMsg retAts = new RetAtsPrivateSaleMsg(qry.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "18005");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("COTYPE", getBankConfig().getValueAsString("coType"));//对接企业类型 1核心企业2服务商
            xmlWriter.writeElementString("SCOCOD", "");//核心企业组织机构代码
            xmlWriter.writeElementString("DLENUM", qry.DealerCode);//经销商代码
            xmlWriter.writeElementString("DLECOD", "");//经销商组织机构代码
            xmlWriter.writeElementString("STADAT", "");//反馈日期起
            xmlWriter.writeElementString("ENDDAT", "");//反馈日期止
            xmlWriter.writeElementString("SURSTS", "04");//监管状态 01在途02在库03私移04私售05正常移动06正常07质损08其它
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),"T18005");//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/RESPONSE/RTNCOD");
                    String tmpRetMsg = tmpBkReader.getFirstText("/RESPONSE/RTNMSG");
                    if (StringHelper.isNullOrEmpty(tmpRetCode)) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        while(tmpBkReader.read("/RESPONSE/DATA")){
                            RetAtsPrivateSaleDetail tmpRetDetail = new RetAtsPrivateSaleDetail();
//                            tmpRetDetail.ID = tmpBkReader.readedXElement.getSingleText("ID");
                            tmpRetDetail.DEALER_CODE = tmpBkReader.readedXElement.getSingleText("DLENUM");
                            tmpRetDetail.DEALER_NAME = tmpBkReader.readedXElement.getSingleText("DLENUM");
                            tmpRetDetail.DISCOVERY_SALE_DATE = DateHelper.convertDateTimetoD8(tmpBkReader.readedXElement.getSingleText("FEDDAT"));
//                            tmpRetDetail.CAR_SERIES_CN = tmpBkReader.readedXElement.getSingleText("CARTYP");
                            tmpRetDetail.CAR_TYPE_CODE = tmpBkReader.readedXElement.getSingleText("CARTYP");
                            tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("CHANUM");
//                            tmpRetDetail.CAR_PRICE = tmpBkReader.readedXElement.getSingleText("CAR_PRICE");
//                            tmpRetDetail.SALE_PLACE = tmpBkReader.readedXElement.getSingleText("SALE_PLACE");
                            tmpRetDetail.REDEMPTION_DATE = tmpRetDetail.DISCOVERY_SALE_DATE;
                            tmpRetDetail.moniStatus = tmpBkReader.readedXElement.getSingleText("SURSTS");
                            tmpRetDetail.warehouseName = tmpBkReader.readedXElement.getSingleText("STRNAM");
                            tmpRetDetail.kchgzh = tmpBkReader.readedXElement.getSingleText("CRELON");
                            tmpRetDetail.kcfdjh = tmpBkReader.readedXElement.getSingleText("ENGENO");
//                            tmpRetDetail.RESERVE4 = tmpBkReader.readedXElement.getSingleText("RESERVE4");
//                            tmpRetDetail.RESERVE5 = tmpBkReader.readedXElement.getSingleText("RESERVE5");
//                            tmpRetDetail.RESERVE6 = tmpBkReader.readedXElement.getSingleText("RESERVE6");
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
